package com.taptech.doufu.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import com.taptech.doufu.R
import com.taptech.doufu.constant.EventKey
import com.taptech.doufu.constant.SPKey
import com.taptech.doufu.event.BaseEventBusBean
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences
import kotlinx.android.synthetic.main.activity_youth_mode_pwd.*
import org.greenrobot.eventbus.EventBus

class YouthModelChangePwdActivity:DiaobaoBaseActivity() {

    private var firstPwd = ""
    private var secondPwd = ""
    private var thirdPwd = ""
    private var pageIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_youth_mode_pwd)
        etPwd.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                when (pageIndex) {
                    0 -> firstPwd = s.toString()
                    1 -> secondPwd = s.toString()
                    else -> thirdPwd = s.toString()
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                when(s?.length){
                    0->{
                        tvPwd1.text = ""
                        tvPwd2.text = ""
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    1->{
                        tvPwd1.text = "*"
                        tvPwd2.text = ""
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    2->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    3->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = "*"
                        tvPwd4.text = ""
                    }
                    4->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = "*"
                        tvPwd4.text = "*"
                    }
                }
            }

        })

        tvTitle.text = "修改密码"
        tvDesc.text = "请输入当前密码"
        btnSure.text = "下一步"
        tvFindPwd.visibility = View.VISIBLE

        tvFindPwd.setOnClickListener {
            YouthModelFindPwdActivity.startActivity(thisActivity)
        }

        btnSure.setOnClickListener {
            if (pageIndex == 0) {
                if (firstPwd.length == 4){
                    val pwd = DiaoBaoSharedPreferences.getSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD, thisActivity, "")
                    if (firstPwd == pwd) {
                        pageIndex = 1
                        etPwd.setText("")
                        tvFindPwd.visibility = View.GONE
                        tvTitle.text = "请输入新密码"
                        tvDesc.text = ""
                        btnSure.text = "确定"
                    }else{
                        etPwd.setText("")
                        firstPwd = ""
                        Toast.makeText(thisActivity,"原密码不正确",Toast.LENGTH_LONG).show()
                    }
                }else{
                    Toast.makeText(thisActivity,"请输入完整密码",Toast.LENGTH_LONG).show()
                }
            }else if (pageIndex == 1) {
                if (secondPwd.length == 4){
                    pageIndex = 2
                    etPwd.setText("")
                    tvTitle.text = "确认新密码"
                    tvDesc.text = ""
                    btnSure.text = "确定"
                }else{
                    Toast.makeText(thisActivity,"请输入完整密码",Toast.LENGTH_LONG).show()
                }
            }else{
                if (thirdPwd.length == 4){
                    if (secondPwd == thirdPwd) {
                        DiaoBaoSharedPreferences.setSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD,thirdPwd,thisActivity)
                        Toast.makeText(thisActivity,"密码修改成功",Toast.LENGTH_LONG).show()
                        finish()
                    }else{
                        etPwd.setText("")
                        thirdPwd = ""
                        Toast.makeText(thisActivity,"两次密码不一致，请重新输入",Toast.LENGTH_LONG).show()
                    }
                }else{
                    Toast.makeText(thisActivity,"请输入完整密码",Toast.LENGTH_LONG).show()
                }
            }
        }

    }


    companion object{

        fun startActivity(context: Context) {
            val intent = Intent()
            intent.setClass(context,YouthModelChangePwdActivity::class.java)
            context.startActivity(intent)
        }
    }
}